package com.playball.lottery.customview;


import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.playball.lottery.R;
import com.playball.lottery.frm.buyhall.BDActivity;

/**
 * 北京单场购彩半全场gridview用到的数据适配器
 * 
 * @author Administrator
 * 
 */
public class BDBqcgridviewAdapter extends BaseAdapter {
	private LayoutInflater inflater;
	private List<PictureBDBQC> pictures;
	private int childPos;
	/** 上下文内容 */
	private Context cxt;
	/**临时存储选择的赛事，由bdActivity来回调*/
	private List<ChildDTO> list_children = new ArrayList<ChildDTO>();
	private List<ChildDTO> list_children_select = new ArrayList<ChildDTO>();

	public BDBqcgridviewAdapter(String[] titles, String[] nums, Context context, List<ChildDTO> appData, int childPosition) {
		super();
		pictures = new ArrayList<PictureBDBQC>();
		inflater = LayoutInflater.from(context);
		if (nums != null) {
			for (int i = 0; i < nums.length; i++) {
				PictureBDBQC picture = new PictureBDBQC(titles[i], nums[i]);
				pictures.add(picture);
			}
		}
		list_children = appData;
		childPos = childPosition;
		cxt=context;
	}

	public int getCount() {
		if (null != pictures) {
			return pictures.size();
		} else {
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return pictures.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolderBDBQC viewHolderBqc;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.buy_jczq_childs_bqcgridview_items, null);
			viewHolderBqc = new ViewHolderBDBQC();
			// 组件ID
			viewHolderBqc.LinearLayout1 = (LinearLayout) convertView.findViewById(R.id.jczq_bqc_RelativeLayout1);
			viewHolderBqc.title = (TextView) convertView.findViewById(R.id.jczq_bqc_cg);
			viewHolderBqc.num = (TextView) convertView.findViewById(R.id.jczq_bqc_sp);
			convertView.setTag(viewHolderBqc);
		} else {
			viewHolderBqc = (ViewHolderBDBQC) convertView.getTag();
		}

		// 赛事按钮点击与否
		// 获得行数据（模型）
		final ChildDTO info = list_children.get(childPos);
		// Log.i("zjq",
		// "展示info.getList_gridview_selectstate()= "+groupPos+" ,childPos= "+childPos+","+info.getList_gridview_selectstate().toString());
		if (((String) ((info.getList_bqcgridview_selectstate()).get(position))).equals("false")) {
//			viewHolderBqc.LinearLayout1.setBackgroundColor(0xffffffff);
			viewHolderBqc.LinearLayout1.setBackgroundResource(R.drawable.ui_new_gv_bg_normal);
			//改变字体颜色
			viewHolderBqc.title.setTextColor(0xff666666);
			viewHolderBqc.num.setTextColor(cxt.getResources().getColor(R.color.ui_new_txt_content_selected));
		} else {
//			viewHolderBqc.LinearLayout1.setBackgroundColor(0xffe34b56);
			viewHolderBqc.LinearLayout1.setBackgroundResource(R.drawable.ui_new_gv_bg_pressed);
			//改变字体颜色
			viewHolderBqc.title.setTextColor(0xffffffff);
			viewHolderBqc.num.setTextColor(0xffffffff);
		}
		final String title = pictures.get(position).getTitle();

		viewHolderBqc.title.setText(title);
		viewHolderBqc.num.setText(pictures.get(position).getNumsp());
		// 按钮点击事件
		viewHolderBqc.LinearLayout1.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (((String) ((info.getList_bqcgridview_selectstate()).get(position))).equals("false")) {
					// 保存按钮选择状态
					List<String> list_grid_btn_lsstate = new ArrayList<String>();
					list_grid_btn_lsstate = info.getList_bqcgridview_selectstate();
					list_grid_btn_lsstate.remove(position);
					list_grid_btn_lsstate.add(position, "true");
					info.setList_bqcgridview_selectstate(list_grid_btn_lsstate);
					
					// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
					List<String> selspslist0 = new ArrayList<String>();
					selspslist0 = info.getList_bqcgridview_selectsp();
					selspslist0.remove(position);
					selspslist0.add(position, pictures.get(position).getNumsp());
					info.setList_bqcgridview_selectsp(selspslist0);

					// 保存按钮内容
					List<String> seltitleslist0 = new ArrayList<String>();
					seltitleslist0 = info.getList_bqcgridview_selectconcent();
					if (seltitleslist0 == null || ((seltitleslist0 != null) && (!seltitleslist0.contains(title)))) {
						seltitleslist0.add(title);
					} else if (seltitleslist0.contains(title)) {
						for (int i = 0; i < seltitleslist0.size(); i++) {
							if (seltitleslist0.get(i).equals(title))
								seltitleslist0.remove(i);
						}
					}
					info.setList_bqcgridview_selectconcent(seltitleslist0);

					// 添加选中项到选中列表中，准备显示到btn
					String str0;
					StringBuffer sb = new StringBuffer();
					for (int i = 0; i < seltitleslist0.size(); i++) {
						sb.append(seltitleslist0.get(i) + "  "); // 2个空格
					}
					str0 = sb.toString();
//					System.out.println("str0= " + str0);
					info.setBtnBQCcontent(str0);
//					// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
//					List<String> selspslist0 = new ArrayList<String>();
//					selspslist0 = info.getList_bqcgridview_selectsp();
//					if (selspslist0 == null || ((selspslist0 != null) && (!selspslist0.contains(pictures.get(position).getNumsp())))) {
//						selspslist0.add(pictures.get(position).getNumsp());
//					} else if (selspslist0.contains(pictures.get(position).getNumsp())) {
//						for (int i = 0; i < selspslist0.size(); i++) {
//							if (selspslist0.get(i).equals(pictures.get(position).getNumsp()))
//								selspslist0.remove(i);
//						}
//					}
//					info.setList_bqcgridview_selectsp(selspslist0);
				} else {
					// 保存按钮选择状态
					List<String> list_grid_btn_lsstate = new ArrayList<String>();
					list_grid_btn_lsstate = info.getList_bqcgridview_selectstate();
					list_grid_btn_lsstate.remove(position);
					list_grid_btn_lsstate.add(position, "false");
					info.setList_bqcgridview_selectstate(list_grid_btn_lsstate);
					
					// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
					List<String> selspslist0 = new ArrayList<String>();
					selspslist0 = info.getList_bqcgridview_selectsp();
					selspslist0.remove(position);
					selspslist0.add(position, "0");
					info.setList_bqcgridview_selectsp(selspslist0);

					// 保存按钮内容,按钮内容没有重复项
					List<String> seltitleslist1 = new ArrayList<String>();
					seltitleslist1 = info.getList_bqcgridview_selectconcent();
					if (seltitleslist1 == null || ((seltitleslist1 != null) && (!seltitleslist1.contains(title)))) {
						seltitleslist1.add(title);
					} else if (seltitleslist1.contains(title)) {
						for (int i = 0; i < seltitleslist1.size(); i++) {
							if (seltitleslist1.get(i).equals(title))
								seltitleslist1.remove(i);
						}
					}
					info.setList_bqcgridview_selectconcent(seltitleslist1);

					// 添加选中项到选中列表中，准备显示到btn
					String str1;
					StringBuffer sb = new StringBuffer();
					for (int i = 0; i < seltitleslist1.size(); i++) {
						sb.append(seltitleslist1.get(i) + "  "); // 2个空格
					}
					str1 = sb.toString();
//					System.out.println("str1= " + str1);
					//反选至一个不选
					if(str1.equals("")){
						info.setBtnBQCcontent("点击弹出下拉列表");
					}else{
						info.setBtnBQCcontent(str1);
					}
//					// 保存用户选择的sp值，供奖金预测使用，前台UI不显示
//					List<String> selspslist1 = new ArrayList<String>();
//					selspslist1 = info.getList_bqcgridview_selectsp();
//					if (selspslist1 == null || ((selspslist1 != null) && (!selspslist1.contains(pictures.get(position).getNumsp())))) {
//						selspslist1.add(pictures.get(position).getNumsp());
//					} else if (selspslist1.contains(pictures.get(position).getNumsp())) {
//						for (int i = 0; i < selspslist1.size(); i++) {
//							if (selspslist1.get(i).equals(pictures.get(position).getNumsp()))
//								selspslist1.remove(i);
//						}
//					}
//					info.setList_bqcgridview_selectsp(selspslist1);
				}

				// 通知btn显示文字改变
				BDActivity.btnChildNamechange();
				//计算已经选择的比赛场数
				// 判断str_sp是否为空，空的话就不回传赋值
				// 再存入List
				list_children_select.add(info);

//				for (ChildDTO child : list_children) {
//					String jc_mid = child.getmId();
//					String str_hteam = child.gethTeam();
//					String str_vteam = child.getvTeam();
//					
//					String str_BtnBQCcontent = child.getBtnBQCcontent();
//					Log.d("JczqBqcgridviewAdapter", "list_children str_BtnBQCcontent=" + str_BtnBQCcontent);
//					Log.d("JczqBqcgridviewAdapter", "list_children jc_mid=" + jc_mid);
//					Log.d("JczqBqcgridviewAdapter", "list_children str_hteam=" + str_hteam);
//					Log.d("JczqBqcgridviewAdapter", "list_children str_vteam=" + str_vteam);
//					
//				}
//				Log.v("JczqBqcgridviewAdapter ", "list_children list_children.size()= " + list_children.size());
				// 回调initMapData这个方法，转存到全局变量里，选号结束
//				JcfootballActivity.initMapData(list_children,"BQC");
				((BDActivity) cxt).initMapData(list_children_select,"BQC");

				// 重新调用getview，不要在此事件直接设置组件的显示
				notifyDataSetChanged();
			}
		});
		return convertView;
	}
}

class ViewHolderBDBQC {
	public TextView title;
	public TextView num;
	public LinearLayout LinearLayout1;
}

class PictureBDBQC {
	private String title;
	private String numsp;

	public PictureBDBQC() {
		super();
	}

	public PictureBDBQC(String title, String numsp) {
		super();
		this.title = title;
		this.numsp = numsp;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getNumsp() {
		return numsp;
	}

	public void setNumsp(String numsp) {
		this.numsp = numsp;
	}

}
